package com.way.gesture.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;

import com.way.gesture.GestureController;
import com.way.gesture.OverviewModeListener;
import com.way.gesture.R;

public class GestureDialog extends Dialog implements View.OnClickListener,
		OverviewModeListener {
	private WindowManager mWindowManager;
	private GestureOverView mGestureOverView;
	private GestureController mGestureController;
	private FrameLayout mRootView;

	public GestureDialog(Context context, GestureController gestureController) {
		super(context, R.style.dialog);
		mGestureController = gestureController;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		if (mGestureController.isOnLockScreen()) {
			getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
		} else {
			getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
		}
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gesture_dialog_layout);
		mRootView = (FrameLayout) findViewById(R.id.gesture_dialog_root);
		mRootView.setOnClickListener(this);
		mWindowManager = (WindowManager) getContext().getSystemService(
				Context.WINDOW_SERVICE);
		mGestureOverView = new GestureOverView(getContext(), mGestureController);
		mGestureOverView.setBackgroundResource(R.drawable.gesture_bg);
		mGestureOverView.setModeChangedListener(this);
		int width = getDisplayWidth();
		int height = getDisplayHeight();
		final int gestureViewWidth = 6 * Math.min(width, height) / 7;
		FrameLayout.LayoutParams frameLayoutParams = new FrameLayout.LayoutParams(
				gestureViewWidth, gestureViewWidth, Gravity.CENTER);
		mRootView.addView(mGestureOverView, frameLayoutParams);
		WindowManager.LayoutParams windowLayoutParams = getWindow()
				.getAttributes();
		windowLayoutParams.width = width;
		windowLayoutParams.height = height;
		getWindow().setAttributes(windowLayoutParams);

	}

	@Override
	public void show() {
		super.show();
		mGestureOverView.setEnabled(true);
		mRootView.startAnimation(createInAnimation());
	}

	@Override
	public void cancel() {
		// super.cancel();
		mRootView.startAnimation(createOutAnimation());
	}

	public AnimationSet createOutAnimation() {
		AnimationSet animationSet = new AnimationSet(true);
		AlphaAnimation alphaAnimation = new AlphaAnimation(1.0F, 1.0F);
		alphaAnimation.setDuration(200L);
		animationSet.addAnimation(alphaAnimation);
		ScaleAnimation scaleAnimation = new ScaleAnimation(1.0F, 0.8F, 1.0F,
				0.8F, 1, 0.5F, 1, 0.5F);
		scaleAnimation.setDuration(200L);
		animationSet.addAnimation(scaleAnimation);
		TranslateAnimation translateAnimation = new TranslateAnimation(0.0F,
				0.0F, 0.0F, -getWindow().getAttributes().height);
		translateAnimation.setDuration(200L);
		animationSet.addAnimation(translateAnimation);
		animationSet.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				GestureDialog.super.cancel();
			}
		});
		return animationSet;
	}

	public AnimationSet createInAnimation() {
		AnimationSet animationSet = new AnimationSet(true);
		AlphaAnimation alphaAnimation = new AlphaAnimation(0.4F, 1.0F);
		alphaAnimation.setDuration(300L);
		animationSet.addAnimation(alphaAnimation);
		ScaleAnimation scaleAnimation = new ScaleAnimation(0.8F, 1.0F, 0.8F,
				1.0F, 1, 0.5F, 1, 0.5F);
		scaleAnimation.setDuration(300L);
		animationSet.addAnimation(scaleAnimation);
		TranslateAnimation translateAnimation = new TranslateAnimation(0.0F,
				0.0F, -getDisplayHeight(), 0.0F);
		translateAnimation.setDuration(300L);
		translateAnimation.setRepeatCount(0);
		animationSet.addAnimation(translateAnimation);
		return animationSet;
	}

	public void reLayout() {
		int width = getDisplayWidth();
		int height = getDisplayHeight();
		final int gestureViewWidth = 6 * Math.min(width, height) / 7;
		FrameLayout.LayoutParams frameLayoutParams = new FrameLayout.LayoutParams(
				gestureViewWidth, gestureViewWidth, Gravity.CENTER);
		mRootView.updateViewLayout(mGestureOverView, frameLayoutParams);
		WindowManager.LayoutParams windowLayoutParams = getWindow()
				.getAttributes();
		windowLayoutParams.width = width;
		windowLayoutParams.height = height;
		getWindow().setAttributes(windowLayoutParams);

	}

	/**
	 * 获取手机屏幕高度
	 * 
	 * @param context
	 * @return
	 */
	public int getDisplayHeight() {
		DisplayMetrics dm = new DisplayMetrics();
		// 获取屏幕信息
		mWindowManager.getDefaultDisplay().getMetrics(dm);
		return dm.heightPixels;
	}

	/**
	 * 获取手机屏幕宽度
	 * 
	 * @param context
	 * @return
	 */
	public int getDisplayWidth() {
		DisplayMetrics dm = new DisplayMetrics();
		// 获取屏幕信息
		mWindowManager.getDefaultDisplay().getMetrics(dm);
		return dm.widthPixels;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.gesture_dialog_root:
			onBackPressed();
			break;

		default:
			break;
		}
	}

	@Override
	public void modeChanged(GestureOverView gestureOverView) {
		switch (gestureOverView.getCurrentMode()) {
		case GestureOverView.MODE_EDIT:

			break;

		default:
			break;
		}
	}

}
